package pl.polidea.imf.common.java.impl.storage;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import pl.polidea.imf.blackberry.util.FileConnectionHelper;
import pl.polidea.imf.common.java.storage.LogStream;
import pl.polidea.imf.common.java.util.StreamHelper;

public class FileLogStream implements LogStream {
	
	String filePath;
	InputStream logInputStream;
	FileConnection fileConnection;
	
	
	public FileLogStream(String filePath) {
		super();
		this.filePath = filePath;
	}

	public void close() {
		FileConnectionHelper.closeConnection(fileConnection);
		
	}

	public InputStream getInputStream() throws IOException {
		logInputStream = fileConnection.openInputStream();
		return logInputStream;
	}

	public void open() throws IOException {
		fileConnection = (FileConnection) Connector.open(filePath, Connector.READ_WRITE);
		
	}

	public void closeAndRemove() throws IOException {
		StreamHelper.closeInputStream(logInputStream);
		fileConnection.delete();
		
	}

}
